package cn.iocoder.yudao.module.alipay.service.babyordername;

import cn.iocoder.yudao.module.alipay.dal.dataobject.babyorder.BabyOrderDO;
import cn.iocoder.yudao.module.alipay.dal.mysql.babyorder.BabyOrderMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.alipay.controller.app.babyordername.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.babyordername.BabyOrderNameDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.alipay.convert.babyordername.BabyOrderNameConvert;
import cn.iocoder.yudao.module.alipay.dal.mysql.babyordername.BabyOrderNameMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.alipay.enums.ErrorCodeConstants.*;

/**
 * 宝宝起名订单名称关联 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BabyOrderNameServiceImpl implements BabyOrderNameService {

    @Resource
    private BabyOrderNameMapper babyOrderNameMapper;

    @Resource
    private BabyOrderMapper babyOrderMapper;

    @Override
    public Long createBabyOrderName(AppBabyOrderNameCreateReqVO createReqVO) {
        // 插入
        BabyOrderNameDO babyOrderName = BabyOrderNameConvert.INSTANCE.convert(createReqVO);
        validateBabyOrderNameExistsByTradeNo(babyOrderName.getTradeNo());
        babyOrderNameMapper.insert(babyOrderName);
        // 返回
        return babyOrderName.getId();
    }

    @Override
    public void updateBabyOrderName(AppBabyOrderNameUpdateReqVO updateReqVO) {
        // 校验存在
        validateBabyOrderNameExists(updateReqVO.getId());
        // 更新
        BabyOrderNameDO updateObj = BabyOrderNameConvert.INSTANCE.convert(updateReqVO);
        babyOrderNameMapper.updateById(updateObj);
    }


    @Override
    public void deleteBabyOrderName(Long id) {
        // 校验存在
        validateBabyOrderNameExists(id);
        // 删除
        babyOrderNameMapper.deleteById(id);
    }

   public void validateBabyOrderNameExistsByTradeNo(String tradeNo){
       if (babyOrderNameMapper.selectByTradeNo(tradeNo) != null) {
           throw exception(BABY_ORDER_NAME_EXISTS);
       }
    };


    private void validateBabyOrderNameExists(Long id) {
        if (babyOrderNameMapper.selectById(id) == null) {
            throw exception(BABY_ORDER_NAME_NOT_EXISTS);
        }
    }

    @Override
    public BabyOrderNameDO getBabyOrderName(Long id) {
        return babyOrderNameMapper.selectById(id);
    }

    @Override
    public List<BabyOrderNameDO> getBabyOrderNameList(Collection<Long> ids) {
        return babyOrderNameMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BabyOrderNameDO> getBabyOrderNamePage(AppBabyOrderNamePageReqVO pageReqVO) {
        return babyOrderNameMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BabyOrderNameDO> getBabyOrderNameList(AppBabyOrderNameExportReqVO exportReqVO) {
        return babyOrderNameMapper.selectList(exportReqVO);
    }

    @Override
    public BabyOrderNameDO getByTradeNo(String tradeNo) {
//        BabyOrderNameDO babyOrderNameDO = babyOrderNameMapper.selectByTradeNo(tradeNo);
//        if(babyOrderNameDO == null){
//            BabyOrderDO babyOrderDO = babyOrderMapper.selectByTradeNo(tradeNo);
//            babyOrderNameDO = new BabyOrderNameDO();
//            babyOrderNameDO.setOutTradeNo(babyOrderNameDO.getOutTradeNo());
//            babyOrderNameDO.setTradeNo(tradeNo);
//            babyOrderNameDO.setNames(babyOrderNameDO.getOutTradeNo());
//
//        }


        return babyOrderNameMapper.selectByTradeNo(tradeNo);
    }

}
